package com.iretailer.report.web.system;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iretailer.report.base.BaseAction;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.dto.ReRole;
import com.iretailer.report.model.*;
import com.iretailer.report.util.RMap;
import com.iretailer.report.util.SecurityUserUtil;
import com.iretailer.report.util.StringUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther: xjw
 * @Date: 2019/1/28 10:19
 * @Description:
 */
@RestController
public class RoleController extends BaseAction {

    @RequestMapping("/system/gridRole.action")
    public Map<String, Object> gridRole(@RequestBody ReRole reRole, HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryOption queryOption = modelMapper.map(reRole, QueryOption.class);
        PageResult<RoleCompany> result = roleService.selectRolePage(user.getRoleId(), reRole.getName(), queryOption);
        return RMap.ok(result);
    }

    @RequestMapping("/system/selectRole.action")
    public Map<String, Object> selectRole(HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        QueryWrapper<Role> qu = new QueryWrapper<>();
        if (!"-".equals(user.getRoleId())) {
            qu.eq("ParentId", user.getRoleId());
        }
        ;
        List<Role> result = roleService.list(qu);
        return RMap.ok(result);
    }

    @RequestMapping("/system/roleMenu.action")
    public Map<String, Object> roleMenu(@RequestBody ReRole reRole,HttpServletRequest request) {
        String roleId = reRole.getId();
         User user = SecurityUserUtil.currentUser(request);
        List<Menus> menuTree = menusService.selectMenuTreeByRoleId(user.getRoleId());
        Map<String, Object> result = new HashMap<>();
        result.put("menuTree", menuTree);
        // select
        if (StringUtil.notEmpty(roleId)) {
            List<RoleMenu> roleMenus = roleMenuService.list(new QueryWrapper<RoleMenu>().lambda().eq(RoleMenu::getRoleId, roleId));
            result.put("menuSelect", roleMenus);
        } else {
            result.put("menuSelect", new ArrayList<>());
        }
        return RMap.ok(result);
    }

    @RequestMapping("/system/saveOrUpdateRole.action")
    public Map<String, Object> saveOrUpdate(@RequestBody ReRole reRole,HttpServletRequest request) {
         User user = SecurityUserUtil.currentUser(request);
        Role role = modelMapper.map(reRole, Role.class);
        boolean result = roleService.saveOrUpdate(role, user.getRoleId(), reRole.getRoleMenu(), user.getName());
        return RMap.resultBoolean(result);
    }


    @RequestMapping("/system/delRole.action")
    public Map<String, Object> delRole(@RequestBody List<String> ids) {
        boolean result = true;
        for (String id : ids) {
            List<User> users = userService.list(new QueryWrapper<User>().lambda().eq(User::getRoleId, id));
            if (users != null && users.size() > 0) {
                result = false;
                break;
            }
        }
        if (result) {
            roleService.deleteByIds(ids);
            return RMap.ok();
        } else {
            return RMap.error(RMap.Code.CONNECTED, "have_children_user");
        }

    }


}
